package cn.demo.text.editor.frame;

import cn.demo.text.editor.constant.RunParam;
import cn.demo.text.editor.constant.Str;
import cn.demo.text.editor.utils.NumberUtils;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

public class PasswordTextField {

    public PasswordTextField() {
        JFrame jf = new JFrame(Str.PASSWORD);

        Container contentPane = jf.getContentPane();
        contentPane.setLayout(new BorderLayout());

        JPanel jp = new JPanel();

        JPasswordField jtf2 = new JPasswordField(30);
        jtf2.setFont(new Font(Str.REGULAR_SCRIPT, Font.BOLD | Font.ITALIC, 30));
        jp.add(jtf2);

        contentPane.add(jp);

        jf.pack();

        jf.setLocationRelativeTo(null);

        jf.setVisible(true);

        jf.addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                String encryptKey = new String(jtf2.getPassword());
                if (!NumberUtils.isNumeric(encryptKey)) {
                    new TipTextField(Str.PWD_SUPPORT_NUMBER);
                } else {
                    RunParam.context.put(Str.SECRET_KEY, encryptKey);
                }

            }
        });

        jtf2.addKeyListener(new KeyAdapter() {
            @Override
            public void keyTyped(KeyEvent e) {
                if (e.getKeyChar() == KeyEvent.VK_ENTER) {
                    String encryptKey = new String(jtf2.getPassword());
                    if (!NumberUtils.isNumeric(encryptKey)) {
                        new TipTextField(Str.PWD_SUPPORT_NUMBER);
                    } else {
                        RunParam.context.put(Str.SECRET_KEY, encryptKey);
                        jf.dispose();
                    }
                }
            }
        });
    }

}
